package com.sh.rpc.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * mock测试
 * @author SH
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

         Class<?> methodReturnType = method.getReturnType();
         log.info("现在是mock测试 {}", method.getName());
        Object obj = getDefaultObject(methodReturnType);
        return obj;
    }

    private Object getDefaultObject(Class<?> methodReturnType) {
        if (methodReturnType.isPrimitive()) {  // 如果是基本数据类型
            if (methodReturnType == int.class) {
                return 0;
            } else if (methodReturnType == long.class) {
                return 0L;
            } else if (methodReturnType == double.class) {
                return 0D;
            } else if (methodReturnType == float.class) {
                return 0F;
            } else if (methodReturnType == boolean.class) {
                return false;
            } else if (methodReturnType == char.class) {
                return '\0';
            }
        }

        // 如果是对象
        return null;
    }
}
